/************************************************
 *** Datei: UIController.cpp
 ***
 *** Erstellt am: 16.05.2012
 ***       Autor: Max Stark <Max.Stark88@googlemail.com>
 ***	 Version: 1.0
 ***	
 *** Beschreibung:
 *** <hier Beschreibung einfuegen>
 ************************************************/

#include "../logic/DroneSwarmController.h"
#include "UIController.h"

UIController::UIController(DroneSwarmController* dsController) {
	this->dsController = dsController;
	this->uiObserverList = new vector<IDroneSwarmUI*>();
}

void UIController::attachUIObserver(IDroneSwarmUI* uiObserver) {
	this->uiObserverList->push_back(uiObserver);
}

// Alle Observer benachrichtigen
void UIController::triggerUpdate(int update) {
	for(size_t i=0; i<this->uiObserverList->size(); ++i)
		this->uiObserverList->at(i)->update(update);
}

void UIController::connectDrone() {
	this->dsController->getDrone()->connectDrone();

	this->triggerUpdateDroneNavDataThread_Active = true;
	this->triggerUpdateDroneNavDataThread = new thread(
				&UIController::triggerUpdateDroneNavData, this);
}

void UIController::disconnectDrone() {
	this->triggerUpdateDroneNavDataThread_Active = false;
	this->triggerUpdateDroneNavDataThread->join();
	delete this->triggerUpdateDroneNavDataThread;

	this->dsController->getDrone()->disconnectDrone();
}

void UIController::triggerUpdateDroneNavData() {
	while(this->triggerUpdateDroneNavDataThread_Active) {
		this->triggerUpdate(UPDATE_DRONENAVDATA);
		this_thread::sleep(boost::posix_time::milliseconds(1000));
	}
}

void UIController::sendMasterMessage(int index) {
	this->dsController->sendMasterMessage(index);
}

vector<DroneListEntry>* UIController::getDroneList() {
	return this->dsController->getDroneList();
}

IDrone* UIController::getDrone() {
	return this->dsController->getDrone();
}

IDroneNavData* UIController::getDroneNavData() {
	return this->dsController->getDrone()->getNavData();
}

UIController::~UIController() {
	delete this->uiObserverList;
}

